#include "DialLookAndFeel.h"

DialLookAndFeel::DialLookAndFeel()
    : knobBackground(juce::ImageCache::getFromMemory(
        BinaryData::knobBackground_png, BinaryData::knobBackground_pngSize))
{
}

void DialLookAndFeel::drawRotarySlider(juce::Graphics& g, int x, int y,
    int width, int height, float sliderPos,
    const float rotaryStartAngle,
    const float rotaryEndAngle,
    juce::Slider&)
{
    float diameter = juce::jmin(width, height);
    float radius = diameter / 2.0f;
    float centreX = x + width / 2.0f;
    float centreY = y + height / 2.0f;
    float rx = centreX - radius;
    float ry = centreY - radius;
    float angle = rotaryStartAngle + (sliderPos * (rotaryEndAngle - rotaryStartAngle));
    auto rw = radius * 2.0f;

    // fill
    juce::Rectangle<float> dialArea(rx + 2.5f, ry + 2.5f, diameter - 5.f,
        diameter - 5.f);
    auto rotation = juce::AffineTransform::rotation(angle, centreX, centreY);
    g.addTransform(rotation);

    g.setColour(contourColour);
    g.fillEllipse(rx, ry, diameter, diameter);
    g.drawImage(knobBackground, dialArea);
    // Outline
}

void DialLookAndFeel::drawToggleButton(juce::Graphics& g,
    juce::ToggleButton& button,
    bool shouldDrawButtonAsHighlighted,
    bool shouldDrawButtonAsDown)
{
    auto fontSize = juce::jmin(15.0f, (float)button.getHeight() * 0.75f);
    auto tickWidth = fontSize * 1.1f;

    juce::Rectangle<float> tickBounds(
        4.0f, ((float)button.getHeight() - tickWidth) * 0.5f, tickWidth,
        tickWidth);

    g.setColour(juce::Colours::white);
    g.drawRoundedRectangle(tickBounds, 4.0f, 1.0f);

    if (button.getToggleState()) {
        g.setColour(contourColour);
        juce::Rectangle<float> tick(4.0f + tickWidth * 0.125f,
            ((float)button.getHeight() - tickWidth) * 0.5f + tickWidth * 0.125f,
            tickWidth * 0.75f, tickWidth * 0.75f);
        g.fillRoundedRectangle(tick, 3.f);
    }
}
